package hive.function;

import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class GetConstellationByBirth extends GenericUDF {

    //初始化星座数组，起始日期每个月份的星座对应相应索引编号
    String[] constellation = {"摩羯座","水瓶座","双鱼座","白羊座","金牛座",
            "双子座","巨蟹座","狮子座", "处女座","天秤座","天蝎座","射手座"};
    //初始化星座数组的起始日期，每个月份对应相应索引编号
    int[] consStartDate = {22,20,19,21,20,21,22,23,23,23,24,23};

    String message = "";

    int month = 0;
    int day = 0;

    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {

        // 判断输入参数的个数
        if(arguments.length !=1){
            throw new UDFArgumentLengthException("Input Args Length Error!!!");
        }
        // 判断输入参数的类型
        if(!arguments[0].getCategory().equals(ObjectInspector.Category.PRIMITIVE)){
            throw new UDFArgumentTypeException(0,"Input Args Type Error!!!");
        }
        //函数本身返回值为String，需要返回String类型的鉴别器对象
        return PrimitiveObjectInspectorFactory.javaStringObjectInspector;
    }


    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {


//        int birthLength = birth.length();
//        //只允许年月日和月日两种情况的数据
//        if(birthLength==5 || birthLength==10){
//            //日期格式可能用 . 或 / 或 - 连接
//            String[] ymd = birth.split("[\\.\\/\\-]");
//
//            //如果数组长度为2，表示的是月日的情况
//            if(ymd.length==2){
//                month = Integer.parseInt(ymd[0]);
//                day = Integer.parseInt(ymd[1]);
//            }else if(ymd.length==3){
//                //如果数组长度为3，表示的是年月日的情况
//                month = Integer.parseInt(ymd[1]);
//                day = Integer.parseInt(ymd[2]);
//            }else{
//                message = "生日格式错误";
//            }
//        }else{
//            message = "生日格式错误";
//        }

        try{
            String date = arguments[0].get().toString();
            LocalDate birthDate = null;
            if(date.length()==10){
                birthDate = LocalDate.parse(date,DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }else if(date.length()==5){
                birthDate = LocalDate.parse(date,DateTimeFormatter.ofPattern("MM-dd"));
            }else{
                message = "生日格式错误";
            }
            month = birthDate.getMonth().getValue();
            day = birthDate.getDayOfMonth();
        }catch (Exception e){
            message = "生日格式错误";
        }

        if(month>0 && day>0){
            message = consStartDate[month%12]>day?constellation[month-1]:constellation[month%12];
        }

        return message;
    }

    @Override
    public String getDisplayString(String[] strings) {
        return "";
    }
}
